package com.hou.shop.product.service.impl;

import com.hou.shop.product.entity.AttrEntity;
import com.hou.shop.product.service.AttrService;
import com.hou.shop.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hou.common.utils.PageUtils;
import com.hou.common.utils.Query;

import com.hou.shop.product.dao.AttrGroupDao;
import com.hou.shop.product.entity.AttrGroupEntity;
import com.hou.shop.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    // 根据id查询属性信息

    /**
     * @param params 传入有关分页信息
     * @param catId  id为0查询所有属性信息,id不为零查询指定id商品属性
     * @return
     */
    @Override
    public PageUtils queryPageById(Map<String, Object> params, Long catId) {

        String key = (String) params.get("key");

        // 条件构造器
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();

        // 判断查询指定catid信息还是全部信息
        if (catId != 0) {
            wrapper.eq("catelog_id", catId);
        }

        // 判断是否进行模糊查询
        if (!StringUtils.isEmpty(key)) {
            wrapper.and((obj) -> {
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }

        // 分页数据查询以及返回
        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
        return new PageUtils(page);
    }

    // 获取分类下所有分组&关联属性
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {

        // 获取全部属性
        List<AttrGroupEntity> catelogs = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<AttrGroupWithAttrsVo> collect = catelogs.stream().map((obj) -> {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(obj, attrGroupWithAttrsVo);

            // 查询该分组id下的分类信息
            List<AttrEntity> relationAttr = attrService.getRelationAttr(attrGroupWithAttrsVo.getAttrGroupId());
            attrGroupWithAttrsVo.setAttrs(relationAttr);

            // 数据返回
            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());
        return collect;
    }

}